<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="名称" prop="nickName">
                <el-input v-model="queryParams.nickName" placeholder="请输入名称" clearable maxlength="10" show-word-limit
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="configurationList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" width="55" type="index" />
            <el-table-column label="头像" align="center" prop="avatar" min-width="100" :show-overflow-tooltip="true">
                <template v-slot="scope">
                    <img :src="scope.row.avatar" alt="" style="width: 40px;height: 40px;">
                </template>
            </el-table-column>
            <el-table-column label="企业名称" align="center" prop="enterpriseName" min-width="100"
                :show-overflow-tooltip="true" />
            <el-table-column label="部门名称" align="center" prop="enterpriseDeptName" min-width="100"
                :show-overflow-tooltip="true" />
            <!-- <el-table-column label="厂区名称" align="center" prop="factoryName" min-width="100"
                :show-overflow-tooltip="true" /> -->
            <el-table-column label="名称" align="center" prop="nickName" min-width="100" :show-overflow-tooltip="true" />
            <el-table-column label="联系电话" align="center" prop="phonenumber" min-width="100"
                :show-overflow-tooltip="true" />
            <el-table-column label="性别" align="center" prop="sex" min-width="100" :show-overflow-tooltip="true">
                <template v-slot="scope">
                    <div v-if="scope.row.sex == 0">男</div>
                    <div v-if="scope.row.sex == 1">女</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="80">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                        v-hasPermi="['member:deptMemebr:record']">评测记录</el-button>
                    <!-- <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                        v-hasPermi="['member:deptMemebr:edit']">修改</el-button> -->
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改配置维护对话框 -->
        <!-- <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="头像" prop="avatar">
                    <image-upload v-model="form.avatar" />
                </el-form-item>
                <el-form-item label="名称" prop="nickName">
                    <el-input v-model="form.nickName" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="联系电话" prop="phonenumber">
                    <el-input v-model="form.phonenumber" placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择性别">
                        <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog> -->


        <!-- 添加或修改配置维护对话框 -->
        <el-dialog title="历史测评报告" :visible.sync="openAppraisal" width="800px" append-to-body>
            <el-table :data="appraisalList">
                <el-table-column label="序号" align="center" width="55" type="index" />
                <el-table-column label="测评名称" align="center" prop="appraisalName" min-width="100"
                    :show-overflow-tooltip="true" />
                <el-table-column label="得分" align="center" prop="appraisalScore" min-width="100"
                    :show-overflow-tooltip="true" />
                <el-table-column label="评测状态" align="center" prop="appraisalStatus" min-width="100"
                    :show-overflow-tooltip="true">
                    <template v-slot="scope">
                        <div v-if="scope.row.appraisalStatus == 0">进行中</div>
                        <div v-if="scope.row.appraisalStatus == 1">已完成</div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"
                    width="80">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="handleViews(scope.row)"
                            v-hasPermi="['member:companyMember:reportView']">详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="appraisalTotal > 0" :total="appraisalTotal" :page.sync="queryParamsAppraisal.pageNum"
                :limit.sync="queryParamsAppraisal.pageSize" @pagination="getuserAppraisalList" />
        </el-dialog>


        <el-dialog title="评测结果分析" :visible.sync="openResult" width="700px" append-to-body>
            <div style="display: flex;justify-content: flex-end;margin: 10px 0;">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="generateReport">导出</el-button>
            </div>

            <vue-html2pdf ref="html2Pdf" :manual-pagination="false" pdf-content-width="100%" :show-layout="false"
                :enable-download="true" :preview-modal="false" :paginate-elements-by-height="3000" :html-to-pdf-options="{
                    margin: 0,
                    filename: '报告.pdf',
                    image: { type: 'jpeg', quality: 0.98 },
                    html2canvas: { scale: 2, useCORS: true },
                    jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }
                }">
                <section slot="pdf-content">
                    <div class="exportPdfData">
                        <scl90-vue ref="scl90Ref" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 8"></scl90-vue>
                        <anxiety-self-assessment ref="anxietyRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 7"></anxiety-self-assessment>
                        <burnout ref="burnoutRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 1">
                        </burnout>
                        <careerInterest ref="careerRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 5">
                        </careerInterest>
                        <childRelationshipAssessment ref="AssessmentRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 10">
                        </childRelationshipAssessment>
                        <childRelationshipDiagnosis ref="DiagnosisRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 4">
                        </childRelationshipDiagnosis>
                        <depressionSelfAssessment ref="depressionRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 2">
                        </depressionSelfAssessment>
                        <maritalQuality ref="maritalRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 9">
                        </maritalQuality>
                        <perceivedStress ref="stressRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 6">
                        </perceivedStress>
                        <socialSupportRatingScale ref="socialSupportRef" :appraisalId="appraisalId" :detailId="detailId"
                            v-if="appraisalId == 3"></socialSupportRatingScale>
                    </div>
                </section>
            </vue-html2pdf>



            <scl90-vue ref="scl90Ref" :appraisalId="appraisalId" :detailId="detailId"
                v-if="appraisalId == 8"></scl90-vue>
            <anxiety-self-assessment ref="anxietyRef" :appraisalId="appraisalId" :detailId="detailId"
                v-if="appraisalId == 7"></anxiety-self-assessment>
            <burnout ref="burnoutRef" :appraisalId="appraisalId" :detailId="detailId" v-if="appraisalId == 1"></burnout>
            <careerInterest ref="careerRef" :appraisalId="appraisalId" :detailId="detailId" v-if="appraisalId == 5">
            </careerInterest>
            <childRelationshipAssessment ref="AssessmentRef" :appraisalId="appraisalId" :detailId="detailId"
                v-if="appraisalId == 10">
            </childRelationshipAssessment>
            <childRelationshipDiagnosis ref="DiagnosisRef" :appraisalId="appraisalId" :detailId="detailId"
                v-if="appraisalId == 4">
            </childRelationshipDiagnosis>
            <depressionSelfAssessment ref="depressionRef" :appraisalId="appraisalId" :detailId="detailId"
                v-if="appraisalId == 2">
            </depressionSelfAssessment>
            <maritalQuality ref="maritalRef" :appraisalId="appraisalId" :detailId="detailId" v-if="appraisalId == 9">
            </maritalQuality>
            <perceivedStress ref="stressRef" :appraisalId="appraisalId" :detailId="detailId" v-if="appraisalId == 6">
            </perceivedStress>
            <socialSupportRatingScale ref="socialSupportRef" :appraisalId="appraisalId" :detailId="detailId"
                v-if="appraisalId == 3"></socialSupportRatingScale>
        </el-dialog>
    </div>
</template>

<script>
import VueHtml2pdf from 'vue-html2pdf'
import scl90Vue from '../cnps/scl90.vue'; //8
import anxietySelfAssessment from '../cnps/anxietySelfAssessment.vue'; //焦虑自我评估7
import burnout from '../cnps/burnout.vue'; //职业倦怠1
import careerInterest from '../cnps/careerInterest.vue'; //霍兰德职业兴趣5
import childRelationshipAssessment from '../cnps/childRelationshipAssessment.vue'; //亲子关系诊断10
import childRelationshipDiagnosis from '../cnps/childRelationshipDiagnosis.vue'; //亲子关系评估4
import depressionSelfAssessment from '../cnps/depressionSelfAssessment.vue'; //抑郁自我评估2
import maritalQuality from '../cnps/maritalQuality.vue'; //婚姻9
import perceivedStress from '../cnps/perceivedStress.vue'; //知觉压力6
import socialSupportRatingScale from '../cnps/socialSupportRatingScale.vue'; //社会支持评分量表3
import { factoryList, memberList, getMember, updateMember, userAppraisalList, getUserAppraisal } from '@/api/member/index.js'
export default {
    name: "ConfigurationDept",
    dicts: ['sys_user_sex'],
    components: {
        VueHtml2pdf,
        scl90Vue,
        anxietySelfAssessment,
        burnout,
        careerInterest,
        childRelationshipAssessment,
        childRelationshipDiagnosis,
        depressionSelfAssessment,
        maritalQuality,
        perceivedStress,
        socialSupportRatingScale
    },
    data() {
        return {
            openResult: false,
            openAppraisal: false,
            appraisalList: [],
            appraisalTotal: 0,
            queryParamsAppraisal: {
                nickName: undefined,
                pageNum: 1,
                pageSize: 10,
                userId: ''
            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 配置维护表格数据
            configurationList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                nickName: undefined,
                pageNum: 1,
                pageSize: 10,
                userType: 2
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                phonenumber: [
                    {
                        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                        message: "请输入正确的手机号码",
                        trigger: "blur"
                    }
                ],
                avatar: [
                    { required: true, message: "头像不能为空", trigger: "blur" }
                ],
                sex: [
                    { required: true, message: "性别不能为空", trigger: "blur" }
                ],
                nickName: [
                    { required: true, message: "名称不能为空", trigger: "blur" }
                ],
            },
            appraisalId: '',
            detailId: ''
        };
    },
    created() {
        this.getList();
    },
    methods: {
        generateReport() {
            const loading = this.$loading({
                lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            setTimeout(() => {
                this.$refs.html2Pdf.generatePdf()
                loading.close();
            }, 2000);
        },
        handleViews(row) {
            this.appraisalId = row.appraisalId;
            this.detailId = row.id;
            this.openResult = true
            // this.$nextTick(() => {
            // 	if (this.$refs.stressRef) {
            // 		this.$refs.stressRef.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.scl90Ref) {
            // 		this.$refs.scl90Ref.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.maritalRef) {
            // 		this.$refs.maritalRef.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.anxietyRef) {
            // 		this.$refs.anxietyRef.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.burnoutRef) {
            // 		this.$refs.burnoutRef.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.careerRef) {
            // 		this.$refs.careerRef.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.AssessmentRef) {
            // 		this.$refs.AssessmentRef.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.DiagnosisRef) {
            // 		this.$refs.DiagnosisRef.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.depressionRef) {
            // 		this.$refs.depressionRef.getDetail(this.detailId);
            // 	}
            // 	if (this.$refs.socialSupportRef) {
            // 		this.$refs.socialSupportRef.getDetail(this.detailId);
            // 	}
            //     this.openResult = true
            // });
        },
        /** 查询配置维护列表 */
        getList() {
            this.loading = true;
            factoryList(this.queryParams).then(response => {
                this.configurationList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                phonenumber: null,
                avatar: null,
                sex: null,
                nickName: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            // this.reset();
            // const id = row.userId
            // getMember(id).then(response => {
            //     this.form = response.data;
            //     this.open = true;
            //     this.title = "修改会员信息";
            // });
            this.appraisalList = []
            this.appraisalTotal = 0
            this.queryParamsAppraisal.userId = row.userId
            this.getuserAppraisalList()
            this.openAppraisal = true
        },
        getuserAppraisalList() {
            userAppraisalList(this.queryParamsAppraisal).then(res => {
                this.appraisalList = res.rows
                this.appraisalTotal = res.total
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    updateMember(this.form).then(response => {
                        this.$modal.msgSuccess("修改成功");
                        this.open = false;
                        this.getList();
                    });
                }
            });
        }
    }
};
</script>
